package com.mbds.P2Pnetwork;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;


public class NetworkP2P {
	private static final String SSID = "sismapp";
	private WifiManager wifiManager;
	private static NetworkP2P singleton;
	
	private NetworkP2P(WifiManager wifiM, Context c) {
		wifiManager = wifiM;
		setUpWifi(c);
	}
	
	public static void init(WifiManager wifiM,Context c) {
		if(singleton == null) {
			singleton = new NetworkP2P(wifiM,c);
		}
	}
	
	public static NetworkP2P getInstance() {
		return singleton;
	}
	
	private void setUpWifi(Context c) {	
		if(wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {		
			connect();
		}		
		
		new Server(wifiManager, c);
	}
	
	private void connect() {
		 WifiConfiguration conf = new WifiConfiguration();
		    conf.SSID = "\"" + SSID + "\""; 
		    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
		    int res =  wifiManager.addNetwork(conf);
		    if(res != -1) {
	        	wifiManager.disconnect();
	        	wifiManager.enableNetwork(res, true);
	        	wifiManager.reconnect();	            
		    }		
	}

}
